package com.yunxi.service.dubboService.impl.vehicle.hubToBaseData;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.yunxi.core.util.StringUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.hubToBaseData.pojo.VehicleConfigReqVO;
import com.yunxi.hubToBaseData.pojo.VehiclePackageReqVO;
import com.yunxi.hubToBaseData.pojo.VehicleSpectrumReqVO;
import com.yunxi.hubToBaseData.service.BaseDataSaveService;
import com.yunxi.model.vehicle.VehicleConfig;
import com.yunxi.model.vehicle.VehicleConfigVer;
import com.yunxi.model.vehicle.VehiclePackage;
import com.yunxi.model.vehicle.VehicleSpectrum;
import com.yunxi.service.localService.vehicle.VehicleConfigService;
import com.yunxi.service.localService.vehicle.VehicleConfigVerService;
import com.yunxi.service.localService.vehicle.VehiclePackageService;
import com.yunxi.service.localService.vehicle.VehicleSpectrumService;
import com.yunxi.util.RedisCacheService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/*@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)*/
public class SToSVehicleConfigDubboServiceImpl implements BaseDataSaveService {
    @Autowired
    private VehicleConfigService vehicleConfigServiceImpl;

    @Autowired
    private VehicleConfigVerService vehicleConfigVerServiceImpl;

    @Autowired
    VehiclePackageService vehiclePackageServiceImpl;

    @Autowired
    VehicleSpectrumService vehicleSpectrumServiceImpl;

    @Autowired
    RedisCacheService redisCacheService;

    /**
     * 工程配置是全量推送
     * @param vo
     * @return
     */
    @Override
    @Transactional
    @LcnTransaction
    public ApiResult saveVehicleConfig(List<VehicleConfigReqVO> vo) {

        if(vo.size() > 0){
            //
            //清空所有数据
            vehicleConfigVerServiceImpl.delAll();
            vehicleConfigServiceImpl.delAll();
        }
        Map<String,VehicleConfig> map = new HashMap<>();

        for(int i = 0; i < vo.size(); i ++){

            if(!map.containsKey(vo.get(i).getFeatureCode())){
                VehicleConfig config = new VehicleConfig();
                BeanUtils.copyProperties(vo.get(i),config);
                map.put(config.getFeatureCode(),config);
            }
        }
        vehicleConfigServiceImpl.saveBatch(map.values());


        List<VehicleConfigVer> configVerList = new ArrayList<>();
        for(int i = 0; i < vo.size(); i ++){
            VehicleConfigVer config = new VehicleConfigVer();
            BeanUtils.copyProperties(vo.get(i),config);
            config.setVehicleCode(vo.get(i).getVechileCode());
            configVerList.add(config);
        }
        vehicleConfigVerServiceImpl.saveBatch(configVerList);

        //vehicleConfigServiceImpl.callPInitProduct();

//        for(int i = 0;i < configVerList.size(); i ++ ){
//            QueryWrapper qw = new QueryWrapper();
//            qw.eq("FEATURE_CODE",configVerList.get(i).getFeatureCode());
//            qw.eq("VEHICLE_CODE",configVerList.get(i).getVehicleCode());
//            VehicleConfigVer ver = vehicleConfigVerServiceImpl.getOne(qw);
//            if(ver != null){
//                vehicleConfigVerServiceImpl.update(configVerList.get(i),qw);
//            }else{
//                vehicleConfigVerServiceImpl.save(configVerList.get(i));
//            }
//        }
//
//        //vehicleConfigVerServiceImpl.saveBatch(configVerList);
//
//        for(int i = 0;i < configList.size(); i ++ ){
//            QueryWrapper qw = new QueryWrapper();
//            qw.eq("FEATURE_CODE",configList.get(i).getFeatureCode());
//            VehicleConfig ver = vehicleConfigServiceImpl.getOne(qw);
//            if(ver != null){
//                vehicleConfigServiceImpl.update(configList.get(i),qw);
//            }else{
//                vehicleConfigServiceImpl.save(configList.get(i));
//            }
//        }
        //vehicleConfigServiceImpl.saveBatch(configList);
        redisCacheService.deleteKeys("vehicle::vehicle:spectrum");
        redisCacheService.deleteKeys("vehicle::vehicle:spectrumAlias");
        redisCacheService.deleteKeys("vehicle::vehicle:spectrumFull");
        redisCacheService.deleteKeys("vehicle");
        return ResponseUtil.ok();
    }

    /**
     * 选装包是增量推送
     * @param vo
     * @return
     */
    @Override
    @Transactional
    @LcnTransaction
    public ApiResult saveVehiclePackage(List<VehiclePackageReqVO> vo) {
        List<VehiclePackage> packageList = new ArrayList<>();
        for(int i = 0;i < vo.size();i ++){
            VehiclePackage entity = new VehiclePackage();
            BeanUtils.copyProperties(vo.get(i),entity);
            packageList.add(entity);
        }

        for(int i = 0;i < packageList.size(); i ++ ){
            QueryWrapper qw = new QueryWrapper();
            qw.eq("FEATURE_CONTENT",packageList.get(i).getFeatureContent());
            qw.eq("FEATURE_CODE",packageList.get(i).getFeatureCode());
            qw.eq("FAMILY_CODE",packageList.get(i).getFamilyCode());
            VehiclePackage ver = vehiclePackageServiceImpl.getOne(qw);
            if(ver != null){
                vehiclePackageServiceImpl.update(packageList.get(i),qw);
            }else{
                vehiclePackageServiceImpl.save(packageList.get(i));
            }
        }
        redisCacheService.deleteKeys("vehicle::vehicle:spectrum");
        redisCacheService.deleteKeys("vehicle::vehicle:spectrumAlias");
        redisCacheService.deleteKeys("vehicle::vehicle:spectrumFull");
        redisCacheService.deleteKeys("vehicle");
        return ResponseUtil.ok();
    }

    /**
     * 车型是全量推送
     * @param vo
     * @return
     */
    @Override
    @Transactional
    @LcnTransaction
    public ApiResult saveVehicleSpectrum(List<VehicleSpectrumReqVO> vo) {

        if(vo.size() > 0){
            vehicleSpectrumServiceImpl.delAll();
        }

        List<VehicleSpectrum> vehicleSpectrums = new ArrayList<>();
        for(int i = 0;i < vo.size();i ++){
            VehicleSpectrum entity = new VehicleSpectrum();
            BeanUtils.copyProperties(vo.get(i),entity);

            if(StringUtils.isEmpty(entity.getParentNodeCode())){
                entity.setParentNodeCode("-1");
            }

            if(StringUtils.isEmpty(entity.getNodeCode())&&StringUtils.isEmpty(entity.getNodeName())){
                return ResponseUtil.fail();
            }else{
                vehicleSpectrums.add(entity);
            }
        }
//        for(int i = 0;i < vehicleSpectrums.size(); i ++ ){
//            QueryWrapper qw = new QueryWrapper();
//            qw.eq("NODE_CODE",vehicleSpectrums.get(i).getNodeCode());
//            VehicleSpectrum  ver = vehicleSpectrumServiceImpl.getOne(qw);
//            if(ver != null){
//                VehicleSpectrum vs = vehicleSpectrums.get(i);
//                if(vs.getNodeCode().equalsIgnoreCase("aiways")){
//                    if(StringUtils.isEmpty(vs.getParentNodeCode())){
//                        vs.setParentNodeCode("-1");
//                    }
//                }
//                vehicleSpectrumServiceImpl.update(vs,qw);
//            }else{
//                vehicleSpectrumServiceImpl.save(vehicleSpectrums.get(i));
//            }
//        }
        vehicleSpectrumServiceImpl.saveBatch(vehicleSpectrums);
        redisCacheService.deleteKeys("vehicle::vehicle:spectrum");
        redisCacheService.deleteKeys("vehicle::vehicle:spectrumAlias");
        redisCacheService.deleteKeys("vehicle::vehicle:spectrumFull");
        redisCacheService.deleteKeys("vehicle");
        return ResponseUtil.ok();
    }
}
